"use strict";
/* Controllers */
app.controller("dqcCheckTaskConfigListController", ['$scope', '$compile', '$location', '$rootScope', '$routeParams', 'dqcCheckTaskConfigListService', function ($scope, $compile, $location, $rootScope, $routeParams, dqcCheckTaskConfigListService) {

    var vm = $scope.vm;

    vm.p = {};
    vm.result = {};

    var errorFn = function (result) {
        alert(result);
    };

    $scope.listDqcCheckTaskConfig = function () {
        var vm = $scope.vm;
        var params = {};
        var procFn = function (response) {
            var data = response.data;
            if (data == null) {
                return;
            }
            vm.result.data = data;
            $('#list_table').bootstrapTable('load', data);
        };
        dqcCheckTaskConfigListService.listDqcCheckTaskConfig(params, procFn, errorFn);
    };




    $scope.confirmDel = function (id, index) {
        vm.toDelId = id;
        vm.toDelIndex = index;
        $('#myModal').modal('show')
    };

    $scope.setDqcCheckTaskStatus = function (ths, id) {
        $(ths).attr("disabled", true);
        var btnText = $(ths).text()
        var params = {};
        params.id = id;
        if (btnText == '启用') {
            params.isOpen = 1;
        } else {
            params.isOpen = 0;
        }
        var procFn = function (response) {
            if (response.success && response.data) {
                if (btnText == '启用') {
                    $(ths).text('停用')
                    $(ths).removeClass('btn-success').addClass('btn-danger')
                } else {
                    $(ths).text('启用')
                    $(ths).removeClass('btn-danger').addClass('btn-success')
                }
                $(ths).attr("disabled", false);
            } else {
                alert(response.msg);
            }
        };
        dqcCheckTaskConfigListService.setDqcCheckTaskStatus(params, procFn, errorFn);
    };

    $scope.initializeController = function () {
        var $resultTable = $('#list_table');
        $resultTable.bootstrapTable({
            pageList: [30, 50, 100, 300, 500, 1000], pageSize: 30
        });
        $resultTable.bootstrapTable('hideColumn', 'createTime');

        $scope.listDqcCheckTaskConfig();
    };

    $scope.initializeController();

}]);



/* Services */
app.service('dqcCheckTaskConfigListService', ['ajaxService', function (ajaxService) {

    this.listDqcCheckTaskConfig = function (params, successFunction, errorFunction) {
        ajaxService.AjaxPost(params, "/star/api/basic-service/v1/dqc/listDqcCheckTaskConfig.do", successFunction, errorFunction);
    };

    this.setDqcCheckTaskStatus = function (params, successFunction, errorFunction) {
        ajaxService.AjaxPost(params, "/star/api/basic-service/v1/dqc/setDqcCheckTaskStatus.do", successFunction, errorFunction);
    };

}]);



function dqcTaskConfigOpt(value, row, index) {
    var btnText = '启用'
    var btnClass = 'success'

    if (row.isOpen == 1) {
        btnText = '停用'
        btnClass = 'danger'
    }
    var optCont = '<div style="width:8rem"><a type="button" class="btn btn-xs btn-' + btnClass + '" onclick=angular.element(this).scope().setDqcCheckTaskStatus(this,' + row.id + ') >' + btnText + '</a>\n'
    var optCont2 = '<a type="button" class="btn btn-xs btn-primary" href="#/sre/dqc/dqc_check_config/' + row.taskType + '/' + row.id + '" target="_blank">修改</a></div>'
    return [
        optCont,
        optCont2
    ].join('');
}

function statusFmt(value, row, index) {
    if (value == 1) {
        return '已启用'
    } else {
        return '已停用'
    }
}


function taskTypeFmt(value, row, index) {
    switch (value) {
        case 'sla_check':
            return 'SLA检查';
        case 'data_count_check':
            return '数据量检查';
        case 'join_radio_check':
            return '串联率检查';
        case 'field_error_check':
            return '字段异常检查';
        default:
            return 'unknown';
    }
    return value;
}

function priorityFmt(value, row, index) {
    if (value == 0) {
        return '高'
    } else {
        return '中'
    }
}

function dpIdFmt(value, row, index) {
    if (value != null && value != '' && typeof ('value') != 'undefined') {
        var optCont2 = '<a href="https://dp.58corp.com/data-develop/task-list/task-detail/' + value + '" target="_blank">' + value + '</a></div>'
        return [
            optCont2
        ].join('');
    } else {
        return '-'
    }

}
